package leetcode.code1024;

import java.util.Arrays;

public class Solution {
	public int videoStitching(int[][] clips, int time) {
		Arrays.sort(clips, (a, b) -> a[0] - b[0]);
		int range = 0, ans = 1, max = 0;
		for (int i = 0; i < clips.length && max < time; i++) {
			int[] c = clips[i];
			int s = c[0], e = c[1];
			if (s <= range) {
				max = Math.max(max, e);
			} else {
				range = max;
				if (s > range) {
					return -1;
				}
				ans++;
				max = e;
			}
		}
		return max >= time ? ans : -1;
	}
}
